package parser.nodes;

// WhileStatement = 'WHILE' Expression 'DO' StatementSequence 'END'

import codegen.CodeGen;

public class WhileStatementNode extends Node {
    private ExpressionNode condition;
    private StatementSequenceNode stmts;

    public WhileStatementNode(ExpressionNode condition, StatementSequenceNode stmts) {
        this.condition = condition;
        this.stmts = stmts;
    }
    
    @Override
    public void print() {
        p("WhileStatement:");
        indent();
        p("WHILE");
        condition.print();
        p("DO");
        stmts.print();
        p("END");
        unindent();
    }

	@Override
	public void abstractPrint() {
		p("WHILE");
		indent();
	    condition.abstractPrint();
		stmts.abstractPrint();
		unindent();
	}
	
	@Override
	public void compile(CodeGen c) throws CompileError{
		int whilelabel = c.getLabel();
		int afterwhile = c.getLabel();
		c.outstr("LABEL, " + whilelabel); 
		condition.compile(c);
		c.outstr("BF, " + afterwhile);
		stmts.compile(c);
		c.outstr("JMP, " + whilelabel);
		c.outstr("LABEL, " + afterwhile);
	}
}
